//
//  SearchTableViewCell.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/29.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "baseImageView.h"
#import "BaseView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface SearchTableViewCell ()

@property (nonatomic, retain) BaseView *viewBack; /**< 背景 */
@property (nonatomic, retain) baseImageView *imgPhoto; /**< 图片 */
@property (nonatomic, retain) baseLabel *labelContent; /**< 内容 */
@property (nonatomic, retain) BaseView *viewLine; /**< 线 */
@property (nonatomic, retain) baseImageView *imgUserPhoto; /**< 用户头像 */
@property (nonatomic, retain) baseLabel *labelUserName; /**< 用户名 */
@property (nonatomic, retain) baseLabel *labelUserContent; /**< 收藏到 */
@property (nonatomic, retain) baseLabel *labelTime; /**< 时间 */

@property (nonatomic, copy) NSNumber *imgWidth;/**<图片宽*/
@property (nonatomic, copy) NSNumber *imgHeight;/**<图片高*/

@property (nonatomic, retain) UIScrollView *scrollView; /**< 背景scrollview */
@property (nonatomic, assign) CGRect originalFrame; /**< 原大小 */
@property (nonatomic, retain) UIImageView *lastImageView; /**< 最近的img */


@end

@implementation SearchTableViewCell
#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_viewBack release];
    [_imgPhoto release];
    [_labelContent release];
    [_viewLine release];
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelUserContent release];
    [_labelTime release];
    [_imgWidth release];
    [_imgHeight release];
    [_scrollView release];
    [_lastImageView release];
   
    [super dealloc];
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;

}
#pragma mark - ------------ 创建控件 ---------------

-(void)create{
    self.viewBack = [[BaseView alloc]init];
    [self.contentView addSubview:_viewBack];
    
    self.viewBack.layer.shadowOffset = CGSizeMake(0.1, 0.1);
    self.viewBack.layer.shadowOpacity = 0.3;
    self.viewBack.layer.shadowRadius = 1.5;
    
    
  
    self.imgPhoto = [[baseImageView alloc]init];
    [self.viewBack addSubview:_imgPhoto];
    
    
    
    
    self.labelContent = [[baseLabel alloc]init];
    [self.viewBack addSubview:_labelContent];
    _labelContent.numberOfLines = 0;
    _labelContent.font = [UIFont systemFontOfSize:14];
   
    
    
    self.viewLine = [[BaseView alloc]init];
    [self.viewBack addSubview:_viewLine];
    
    
    self.imgUserPhoto = [[baseImageView alloc]init];
    [self.viewBack addSubview:_imgUserPhoto];
    _imgUserPhoto.layer.cornerRadius = 25;
    _imgUserPhoto.clipsToBounds = YES;
    
    
    
    self.labelUserName = [[baseLabel alloc]init];
    [self.viewBack addSubview:_labelUserName];
    _labelUserName.font = [UIFont systemFontOfSize:15];
    _labelUserName.textColor = [UIColor colorWithRed:0.00 green:0.61 blue:0.84 alpha:1.00];
    
    self.labelUserContent = [[baseLabel alloc]init];
    [self.viewBack addSubview:_labelUserContent];
    _labelUserContent.font = [UIFont systemFontOfSize:14];
    _labelUserContent.textColor = [UIColor lightGrayColor];
    
    
    self.labelTime = [[baseLabel alloc]init];
    [self.viewBack addSubview:_labelTime];
    _labelTime.textAlignment = NSTextAlignmentRight;
    _labelTime.font = [UIFont systemFontOfSize:13];
    _labelTime.textColor = [UIColor lightGrayColor];
    
    
    // _viewBack.backgroundColor = [UIColor whiteColor];
   
    
        //  _imgPhoto.backgroundColor = [UIColor yellowColor];
    // _labelContent.backgroundColor = [UIColor redColor];
    
    
    //_imgUserPhoto.backgroundColor = [UIColor yellowColor];
    // _labelUserName.backgroundColor = [UIColor blueColor];
    // _labelTime.backgroundColor = [UIColor redColor];
    // _labelUserContent.backgroundColor = [UIColor orangeColor];
    
    
    [_viewBack release];
    [_imgPhoto release];
    [_labelContent release];
    [_viewLine release];
    [_imgUserPhoto release];
    [_labelUserName release];
    [_labelUserContent release];
    [_labelTime release];
    [_imgWidth release];
    [_imgHeight release];
}



-(void)setDic:(NSDictionary *)dic{
   
    if (_dic != dic) {
        [_dic release];
        _dic = [dic retain];
    }
    self.imgWidth = _dic[@"photo"][@"width"];
    self.imgHeight = _dic[@"photo"][@"height"];
     [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:[_dic[@"photo"][@"path"] stringByReplacingOccurrencesOfString:@"_webp" withString:@""]]];
    _labelContent.text = _dic[@"msg"];
    [_imgUserPhoto sd_setImageWithURL:[NSURL URLWithString:_dic[@"sender"][@"avatar"]]];
    _labelUserName.text = _dic[@"sender"][@"username"];

    _labelTime.text = _dic[@"add_datetime"];
}

#pragma mark - ------------ layout 调用 ---------------




-(void)layoutSubviews{
    [super layoutSubviews];
    if (_dic ) {
        [self createframe];
    }

}
#pragma mark - ------------ 设置大小 ---------------

-(void)createframe{
   
    
    //获取图片高度
    CGFloat imgH = [[self class] heightWithPhoto:_imgWidth height:_imgHeight];
    //获取文字高度
    CGFloat textH = [[self class] heightWithText:_labelContent.text];
    
    //背景
    [_viewBack mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo( imgH + textH + 91);
    }];
    
    //图片
    [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self.viewBack).offset(0);
        make.height.mas_equalTo(imgH);
    }];
    
    
    //文字
    [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
        make.left.mas_equalTo(_viewBack).offset(10);
        make.right.mas_equalTo(_viewBack).offset(-10);
        make.height.mas_equalTo(textH);
    }];
    
    //线
    [_viewLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self.viewBack).offset(0);
        make.top.mas_equalTo(_labelContent.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    
    //用户头像
    [_imgUserPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.viewLine.mas_bottom).offset(10);
        make.left.mas_equalTo(self.viewBack).offset(10);
        make.width.height.mas_equalTo(50);
        
    }];
    
    //用户名
    [_labelUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgUserPhoto);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(25);
    }];
    //收集到
    [_labelUserContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_labelUserName.mas_bottom).offset(0);
        make.left.mas_equalTo(_labelUserName);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(_labelUserName);
    }];
    
    //时间
    [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgUserPhoto);
        make.right.mas_equalTo(_viewBack).offset(-10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(30);
        
    }];

}

#pragma mark - ------------ 图片高度自适应 ---------------

+(CGFloat)heightWithPhoto:(NSNumber *)width height:(NSNumber *)height{

   
    CGFloat h = (SCREEN_WIDTH - 20) * height.doubleValue / width.doubleValue;
    return h;
}

#pragma mark - ------------ 文本高度自适应 ---------------

+ (CGFloat)heightWithText:(NSString *)text{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH -40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height;

}





@end
